package com.sxt.bus.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.util.CellRangeAddress;

import com.sxt.bus.domain.Car;
import com.sxt.bus.domain.Customer;
import com.sxt.bus.domain.Rent;

/**
 * 导出租单数据
 * 
 * @author LJH
 * 
 */
public class ExportRentUtils {

	/**
	 * 导出租单数据
	 * 
	 * @param customers
	 * @param fileName
	 */
	@SuppressWarnings("deprecation")
	public static void export(Rent rent,Customer customer,Car car, String fileName,HttpServletResponse response) {
		// 创建工作簿
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 创建sheet
		HSSFSheet sheet = workbook.createSheet(customer.getCustname()+"的出租单信息");
		sheet.setDefaultColumnWidth(30);
		sheet.setColumnWidth(1, 45*256);
		// 合并
		CellRangeAddress region1 = new CellRangeAddress(0, 0, 0, 3);
		sheet.addMergedRegion(region1);
		// 得到样式
		HSSFCellStyle titleStyle = HSSFStyleUtils.getTitleStyle(workbook);
		HSSFCellStyle tableBodyStyle = HSSFStyleUtils
				.getTableBodyStyle(workbook);

		// 创建第一行
		int row = 0;
		HSSFRow row1 = sheet.createRow(row);
		HSSFCell row1_cell1 = row1.createCell(0);
		row1_cell1.setCellStyle(titleStyle);
		row1_cell1.setCellValue(customer.getCustname()+"的出租单信息");

		//创建第二行
		row++;
		HSSFRow row2 = sheet.createRow(row);
		row2.setHeightInPoints((short)150);
		HSSFCell row2_cell1 = row2.createCell(0);
		row2_cell1.setCellStyle(tableBodyStyle);
		row2_cell1.setCellValue("出租单号:");
		
		HSSFCell row2_cell2 = row2.createCell(1);
		row2_cell2.setCellStyle(tableBodyStyle);
		row2_cell2.setCellValue(rent.getRentid());
		
		HSSFCell row2_cell3 = row2.createCell(2);
		row2_cell3.setCellStyle(tableBodyStyle);
		row2_cell3.setCellValue("二维码:");
		
		HSSFCell row2_cell4 = row2.createCell(3);
		row2_cell4.setCellStyle(tableBodyStyle);
		row2_cell4.setCellValue("");
		
		InputStream logoStream=ExportRentUtils.class.getResourceAsStream("/logo.jpg");
		System.out.println(logoStream);
		BufferedImage image=ZXingCodeEncodeUtils.createZxingCodeBufferImage_Logo(rent.getRentid(), 300, 300, "gif", logoStream);
		//创建内存流对象
		ByteArrayOutputStream byteArrayOut=new ByteArrayOutputStream();
		try {
			//把image里面的数据源写到byteArrayOut内存流里面
			ImageIO.write(image, "jpeg", byteArrayOut);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		 //画图的顶级管理器，一个sheet只能获取一个（一定要注意这点）  
		 HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); 
		//anchor主要用于设置图片的属性  
		 /**
			dx1：起始单元格的x偏移量，如例子中的255表示直线起始位置距A1单元格左侧的距离；
			dy1：起始单元格的y偏移量，如例子中的125表示直线起始位置距A1单元格上侧的距离；
			dx2：终止单元格的x偏移量，如例子中的1023表示直线起始位置距C3单元格左侧的距离；
			dy2：终止单元格的y偏移量，如例子中的150表示直线起始位置距C3单元格上侧的距离；
			col1：起始单元格列序号，从0开始计算；
			row1：起始单元格行序号，从0开始计算，如例子中col1=0,row1=0就表示起始单元格为A1；
			col2：终止单元格列序号，从0开始计算；
			row2：终止单元格行序号，从0开始计算，如例子中col2=2,row2=2就表示起始单元格为C3；
		  */
       HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 255,(short) 3, 1, (short) 4, 1);     
       anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);  
       patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));   
		
		//创建第三行
		row++;
		HSSFRow row3 = sheet.createRow(row);
		HSSFCell row3_cell1 = row3.createCell(0);
		row3_cell1.setCellStyle(tableBodyStyle);
		row3_cell1.setCellValue("客户姓名:");
		
		HSSFCell row3_cell2 = row3.createCell(1);
		row3_cell2.setCellStyle(tableBodyStyle);
		row3_cell2.setCellValue(customer.getCustname());
		
		HSSFCell row3_cell3 = row3.createCell(2);
		row3_cell3.setCellStyle(tableBodyStyle);
		row3_cell3.setCellValue("身份证号:");
		
		HSSFCell row3_cell4 = row3.createCell(3);
		row3_cell4.setCellStyle(tableBodyStyle);
		row3_cell4.setCellValue(customer.getIdentity());
		//创建第四行
		row++;
		HSSFRow row4 = sheet.createRow(row);
		HSSFCell row4_cell1 = row4.createCell(0);
		row4_cell1.setCellStyle(tableBodyStyle);
		row4_cell1.setCellValue("起租时间:");
		
		HSSFCell row4_cell2 = row4.createCell(1);
		row4_cell2.setCellStyle(tableBodyStyle);
		row4_cell2.setCellValue(rent.getBegindate().toLocaleString());
		
		HSSFCell row4_cell3 = row4.createCell(2);
		row4_cell3.setCellStyle(tableBodyStyle);
		row4_cell3.setCellValue("还车时间:");
		
		HSSFCell row4_cell4 = row4.createCell(3);
		row4_cell4.setCellStyle(tableBodyStyle);
		row4_cell4.setCellValue(rent.getReturndate().toLocaleString());
		//创建第五行
		row++;
		HSSFRow row5 = sheet.createRow(row);
		HSSFCell row5_cell1 = row5.createCell(0);
		row5_cell1.setCellStyle(tableBodyStyle);
		row5_cell1.setCellValue("车辆号牌:");
		
		HSSFCell row5_cell2 = row5.createCell(1);
		row5_cell2.setCellStyle(tableBodyStyle);
		row5_cell2.setCellValue(car.getCarnumber());
		
		HSSFCell row5_cell3 = row5.createCell(2);
		row5_cell3.setCellStyle(tableBodyStyle);
		row5_cell3.setCellValue("出租价格:");
		
		HSSFCell row5_cell4 = row5.createCell(3);
		row5_cell4.setCellStyle(tableBodyStyle);
		row5_cell4.setCellValue(rent.getPrice());
		//创建第六行
		row++;
		//创建第七行
		row++;
		HSSFRow row7 = sheet.createRow(row);
		HSSFCell row7_cell3 = row7.createCell(2);
		row7_cell3.setCellStyle(tableBodyStyle);
		row7_cell3.setCellValue("打印时间:");
		
		HSSFCell row7_cell4 = row7.createCell(3);
		row7_cell4.setCellStyle(tableBodyStyle);
		row7_cell4.setCellValue(new Date().toLocaleString());
		//创建第八行
		row++;
		HSSFRow row8 = sheet.createRow(row);
		HSSFCell row8_cell3 = row8.createCell(2);
		row8_cell3.setCellStyle(tableBodyStyle);
		row8_cell3.setCellValue("操作员:");
		
		HSSFCell row8_cell4 = row8.createCell(3);
		row8_cell4.setCellStyle(tableBodyStyle);
		row8_cell4.setCellValue(rent.getOpername());
		//创建第九行
		row++;
		HSSFRow row9 = sheet.createRow(row);
		HSSFCell row9_cell3 = row9.createCell(2);
		row9_cell3.setCellStyle(tableBodyStyle);
		row9_cell3.setCellValue("客户签名:");
		
		HSSFCell row9_cell4 = row9.createCell(3);
		row9_cell4.setCellStyle(tableBodyStyle);
		row9_cell4.setCellValue("");
		
		// 处理文件名
		try {
			fileName = URLEncoder.encode(fileName, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		response.reset();
		// 处理文件类型
		response.setContentType("application/x-download");
		// 设置下载的文件名
		response.setHeader("Content-Disposition", "attachment; filename=\""
				+ fileName + "\"");
		try {
			ServletOutputStream outputStream = response.getOutputStream();
			workbook.write(outputStream);
			workbook.close();
			outputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
